/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.dqsy.zty.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.dqsy.zty.entity.Zty;
import com.jeeplus.modules.dqsy.zty.mapper.ZtyMapper;
import com.jeeplus.modules.dqsy.zty.entity.ZtyFubiao;
import com.jeeplus.modules.dqsy.zty.mapper.ZtyFubiaoMapper;

/**
 * 祝福表Service
 * @author 于金洋
 * @version 2020-10-30
 */
@Service
@Transactional(readOnly = true)
public class ZtyService extends CrudService<ZtyMapper, Zty> {

	@Autowired
	private ZtyFubiaoMapper ztyFubiaoMapper;

	public Zty get(String id) {
		Zty zty = super.get(id);
		zty.setZtyFubiaoList(ztyFubiaoMapper.findList(new ZtyFubiao(zty)));
		return zty;
	}

	public List<Zty> findList(Zty zty) {
		return super.findList(zty);
	}

	public Page<Zty> findPage(Page<Zty> page, Zty zty) {
		return super.findPage(page, zty);
	}

	@Transactional(readOnly = false)
	public void save(Zty zty) {
		super.save(zty);
		for (ZtyFubiao ztyFubiao : zty.getZtyFubiaoList()){
			if (ztyFubiao.getId() == null){
				continue;
			}
			if (ZtyFubiao.DEL_FLAG_NORMAL.equals(ztyFubiao.getDelFlag())){
				if (StringUtils.isBlank(ztyFubiao.getId())){
					ztyFubiao.setTestDataMain(zty);
					ztyFubiao.preInsert();
					ztyFubiaoMapper.insert(ztyFubiao);
				}else{
					ztyFubiao.preUpdate();
					ztyFubiaoMapper.update(ztyFubiao);
				}
			}else{
				ztyFubiaoMapper.delete(ztyFubiao);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(Zty zty) {
		super.delete(zty);
		ztyFubiaoMapper.delete(new ZtyFubiao(zty));
	}

}